# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
from meizitu.items import MeizituItem, MeizituPicturesUrlsItem
import re
import requests


class MeizituPipeline(object):
    def process_item(self, item, spider):
        if isinstance(item, MeizituItem):
            self._process_meizitu_item(item)
        else:
            self._process_picture_item(item)
        return item

    def _process_meizitu_item(self, item):
        # 获取目录名称（与标题名称一致）

        if "dir_info" in item:
            dir_name = item['dir_info']
            dir_name = re.sub(r'\s+|_|@|：|:|，|,|！|!|\?|？|\.|_|@', "", dir_name.strip())
            # print("="*9)
            # print(dir_name)
        if "url" in item:
            url = item["url"]

        if not os.path.exists("beauty/" + dir_name):
            os.makedirs("beauty/" + dir_name)

        # if not os.path.exists("beauty/"):
        #     os.makedirs("beauty/")
        #     with open("beauty/urls.txt", "a") as f:
        #         f.write(url + "\n")
        # else:
        #     with open("beauty/urls.txt", "a") as f:
        #         f.write(url + "\n")

    def _process_picture_item(self, item):
        # 获取文件名
        if 'pic_info' in item:
            # print("="*20)
            # print(item['pic_info'])
            if len(item["pic_info"]) == 3:
                pic_headers = item["pic_info"][0]
                pic_url = item["pic_info"][1]
                pic_name = item["pic_info"][2]

                pic_name_clean = re.match(r"(.*)[^（\d+）]", pic_name, re.S).group(0)
                dir_name = re.sub(r'\s+|_|@|：|:|，|,|！|!|\?|？|\.|_', "", pic_name_clean.strip())
                pic_name_new = re.sub(r'\s+|_|@|：|:|，|,|！|!|\?|？|\.', "", pic_name)

                # print("="*20)
                # print(dir_name)
                # print(pic_name_new)
		        # 发送请求
                respon = requests.get(pic_url, headers=pic_headers, stream=True)

		        # 用处理之后的文件夹名与通过标题设置的文件夹名对比，若相同，则写入该文件夹
                if os.path.exists("beauty/" + dir_name):
                    pic_path = "beauty/" + dir_name + "/" + pic_name_new + ".jpeg"
                    # print("="*20)
                    # print(pic_path)
                    if respon:
                        self.save_pics(pic_path, respon)
                else:
        		# 若无同名文件夹（正则匹配中的少量纰漏），则将图片存入others文件夹中
                    pic_path = "beauty/others/" + pic_name_new + ".jpeg"
                    if not os.path.exists("beauty/others"):
                        os.makedirs("beauty/others")
                        self.save_pics(pic_path, respon)
                    else:
                        self.save_pics(pic_path, respon)

    def save_pics(self, pic_path, respon):
        """	保存图片
        """
        with open(pic_path, "wb") as f:
            for chunk in respon.iter_content(chunk_size=1024):
                f.write(chunk)
                f.flush()
